﻿using System;
using System.Drawing;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;

namespace HangameAuto02
{
    public partial class AutoForm : BaseForm
    {

        private short m_HotkeyID;

        public Thread _thread01;

        private bool _Exit = false; 

        public AutoForm()
        {
            InitializeComponent();
        }

        private void btnPokerChannelStart_Click(object sender, EventArgs e)
        {
            this.Hide();
            this._thread01 = new Thread(new ThreadStart(AutoChannel));
            this._thread01.Start();
        }

        private void AutoChannel()
        {
            RECT rect = new RECT();
            Common.SetGameProcess(GameKind.Poker7);
            Bitmap imgCurrent = Common.GetHangameImage();

            if (NativeMethods.GetWindowRect(Common.ptrHanGame, ref rect))
            {
                int[] nCompare = new int[] { 292, 167 };
                Color curColor = imgCurrent.GetPixel(rect.Left + nCompare[0], rect.Top + nCompare[1]);
                double[] current = Common.GetInputPoint(MousePosition.X, MousePosition.Y);
                double[] refresh = Common.GetInputPoint(rect.Left + Properties.Poker7.Default.새로고침.X, rect.Top + Properties.Poker7.Default.새로고침.Y);
                double[] room1In = Common.GetInputPoint(rect.Left + Properties.Poker7.Default.채널01.X, rect.Top + Properties.Poker7.Default.채널01.Y);

                NativeMethods.SetForegroundWindow(Common.ptrHanGame);
                Common.AutoChannelIn(refresh, room1In, current, nCompare, rect, curColor);

            }
        }


        public  void BaseForm_Load(object sender, System.EventArgs e)
        {
            string atomName = Thread.CurrentThread.ManagedThreadId.ToString("X8") + this.GetType().FullName;
            m_HotkeyID = NativeMethods.GlobalAddAtom(atomName);
            NativeMethods.RegisterHotKey(Handle, m_HotkeyID, (int)HangameAuto02.ModifierKeys.MOD_CONTROL, (int)Keys.D);
        }


        public  void BaseForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            NativeMethods.UnregisterHotKey(Handle, m_HotkeyID);
            NativeMethods.GlobalDeleteAtom(m_HotkeyID);

            Dispose();
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message msg)
        {

            switch (msg.Msg)
            {
                case Consts.WM_HOTKEY:
                    if (this._thread01 != null && this._thread01.IsAlive)
                        this._thread01.Abort();
                    this.Show();
                    break;
                default:
                    base.WndProc(ref msg);
                    break;
            }
        }


        private void AutoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this._thread01 != null)
                this._thread01.Abort();

            if (!this._Exit)
            {
                this.Hide();
                e.Cancel = true;
            }
        }

        private void btnClickerStart_Click(object sender, EventArgs e)
        {
            MessageBox.Show("자동으로 클릭할곳을 선택하세요. 취소하고 싶을땐 (CTROL + D)");
            base.OnMouseClickeHook = true;

        }

        public override void HookManager_MouseClick(object sender, MouseEventArgs e)
        {
            base.OnMouseClickeHook = false;
            this.Hide();
            this._thread01 = new Thread(new ParameterizedThreadStart(AutoClick));
            this._thread01.Start(e.Location);
            
        }

        private void AutoClick(object pPoint)
        {
            Common.AutoClick((Point)pPoint);
        }

        private void 종료ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._Exit = true;
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();
            this._thread01 = new Thread(new ThreadStart(AutoChannelBaduki));
            this._thread01.Start();
        }

        private void AutoChannelBaduki()
        {
            RECT rect = new RECT();
            Common.SetGameProcess(GameKind.Baduk);
            Bitmap imgCurrent = Common.GetHangameImage();

            if (NativeMethods.GetWindowRect(Common.ptrHanGame, ref rect))
            {
                int[] nCompare = new int[] { 292, 167 };
                Color curColor = imgCurrent.GetPixel(rect.Left + nCompare[0], rect.Top + nCompare[1]);
                double[] current = Common.GetInputPoint(MousePosition.X, MousePosition.Y);
                double[] refresh = Common.GetInputPoint(rect.Left + Properties.Baduki.Default.새로고침.X, rect.Top + Properties.Baduki.Default.새로고침.Y);
                double[] room1In = Common.GetInputPoint(rect.Left + Properties.Baduki.Default.채널01.X, rect.Top + Properties.Baduki.Default.채널01.Y);

                NativeMethods.SetForegroundWindow(Common.ptrHanGame);
                Common.AutoChannelIn(refresh, room1In, current, nCompare, rect, curColor);

            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.Hide();
            this._thread01 = new Thread(new ThreadStart(AutoChannelHighLow));
            this._thread01.Start();
        }

        private void AutoChannelHighLow()
        {
            RECT rect = new RECT();
            Common.SetGameProcess(GameKind.Highlow);
            Bitmap imgCurrent = Common.GetHangameImage();

            if (NativeMethods.GetWindowRect(Common.ptrHanGame, ref rect))
            {
                int[] nCompare = new int[] { 540, 267 };
                Color curColor = imgCurrent.GetPixel(rect.Left + nCompare[0], rect.Top + nCompare[1]);
                double[] current = Common.GetInputPoint(MousePosition.X, MousePosition.Y);
                double[] refresh = Common.GetInputPoint(rect.Left + Properties.HighLow.Default.새로고침.X, rect.Top + Properties.HighLow.Default.새로고침.Y);
                double[] room1In = Common.GetInputPoint(rect.Left + Properties.HighLow.Default.채널01.X, rect.Top + Properties.HighLow.Default.채널01.Y);

                NativeMethods.SetForegroundWindow(Common.ptrHanGame);
                Common.AutoChannelIn(refresh, room1In, current, nCompare, rect, curColor);

            }
        }


    }
}
